import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';

/// @date 2020/08/06  10:59
/// @author 程志强
/// @des 缩放

class GestureRecognizer extends StatefulWidget {
  @override
  _GestureRecognizerState createState() => _GestureRecognizerState();
}

class _GestureRecognizerState extends State<GestureRecognizer> {
  TapGestureRecognizer _recognizer = TapGestureRecognizer();

  // 变色开关
  bool _toggle = false;

  @override
  void initState() {
    _recognizer.onTap = () {
      setState(() => _toggle = !_toggle);
    };
    super.initState();
  }

  @override
  void dispose() {
    // 用到GestureRecognizer的话一定要调用其dispose方法释放资源
    _recognizer.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text("GestureRecognizer"),
        ),
        body: Center(
          child: Text.rich(TextSpan(children: [
            TextSpan(text: "你好世界"),
            TextSpan(
                text: "点我变色",
                style: TextStyle(
                    fontSize: 30.0, color: _toggle ? Colors.blue : Colors.red),
                recognizer: _recognizer
            ),
            TextSpan(text: "你好世界"),
          ])),
        ));
  }
}
